﻿package ui {
	import com.greensock.easing.Bounce;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Elastic;
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	import flash.filters.BevelFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	public class Button extends UIItem {
		public var toggled:Boolean = false;
		public var togglable:Boolean = false;
		private var a:Number = NaN;
		private var txt:TextField = new TextField();
		public function Button(title:String, options:Object = null) {
			applyOptions(options);
			
			txt.defaultTextFormat = new TextFormat("Tahoma", 9, 0xFFFFFF, true);
			txt.text = title;
			txt.x = 3;
			txt.y = 3;
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.selectable = txt.tabEnabled = txt.mouseEnabled = false;
			addChild(txt);
			
			buttonMode = true;
			
			if (tabIndex == -1) tabIndex = nextTabIndex++;
			
			//filters = [new BevelFilter(2, 45, 0xFFFFFF, 0.5, 0x000000, 0.5, 4, 4, 1), new GlowFilter(0x000000, 0.5, 2, 2, 1)];
			//filters = [new GlowFilter(0x000000, 0.2)];
			
			addEventListener(MouseEvent.ROLL_OVER, rollOver);
			addEventListener(MouseEvent.ROLL_OUT, rollOut);
			if (togglable) {
				redrawToggle();
			} else {
				redraw(0.8);
			}
		}
		
		public function get text():String {
			return txt.text;
		}
		public function set text(v:String):void {
			txt.text = v;
			redraw();
		}
		
		public function setToggle(t:Boolean, delay:Number = 0):Boolean {
			var changed:Boolean = toggled != t;
			toggled = t;
			redrawToggle();
			if (changed) {
				var prev:Matrix = transform.matrix;
				var timeline:TimelineLite = new TimelineLite();
				timeline.append(new TweenLite(this, 0.05, { delay: delay, alpha: 0.1, ease: Cubic.easeIn } ));
				timeline.append(new TweenLite(this, 0.5, { alpha: alpha, ease: Cubic.easeOut } ));
				//timeline.append(new TweenLite(this, 1, { z: 0, ease: Elastic.easeOut, onCompleteParams: [this], onComplete: function(d:DisplayObject):void { d.cacheAsBitmap = false } } ));
				//timeline.append(new TweenLite(this, 0.3, { z: 0, ease: Elastic.easeOut, onComplete: function():void { transform.matrix = prev; } } ));
				
			}
			return changed;
		}
		public function isToggled():Boolean {
			return toggled;
		}
		
		public function toggle():void {
			if (togglable) toggled = !toggled;
			redrawToggle();
		}
		
		private function rollOver(e:MouseEvent):void {
			redraw(togglable ? (toggled ? 0.6 : 0.8) : 1);
		}
		
		private function rollOut(e:MouseEvent):void {
			redraw(togglable ? (toggled ? 1 : 0.5) : 0.8);
		}
		
		private function redrawToggle():void {
			redraw(toggled ? 1 : 0.5);
		}
		
		private function redraw(a:Number = NaN):void {
			if (isNaN(a)) {
				a = this.a;
			} else {
				this.a = a;
			}
			
			var g:Graphics = graphics;
			g.clear();
			g.beginFill(0xFFFFFF);
			g.drawRoundRect(0, 0, txt.x*2+txt.width, txt.y*2+txt.height, 6, 6);
			g.beginFill(0x4477FD, a);
			g.drawRoundRect(0, 0, txt.x*2+txt.width, txt.y*2+txt.height, 6, 6);
			/*
			g.beginFill(0xFFFFFF);
			g.drawRect(0, 0, txt.x*2+txt.width, txt.y*2+txt.height);
			g.beginFill(0x4477FD, a);
			g.drawRect(0, 0, txt.x*2+txt.width, txt.y*2+txt.height);
			*/
		}
	}
	
}